<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $framework;
require_once($framework."/debug.php");
require($framework."/configuration.php");
/**
 * Internationalization of any translatable string in the code: user infomation, 
 * or sentence to be displayed on page.
 * @author frederic delorme
 *
 */
class I18n{
	static private $_instance;
	private $messages = array();
	private $active_language = "en_EN";
	private $encode = "UTF-8";
	/**
	 * Initialisation of the internationalization module.
	 * @return unknown_type
	 */
	public function __construct(){
		// retrieve session information or load default value from config.
		if(Session::get("i18n")){
			$this->active_language = Session::get("i18n");
			__debug(__CLASS__."::__construct() - active_language from PHP session:".$this->active_language,__FILE__,__LINE__);
		}else if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
			$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
			$this->active_language = str_replace('-','_',$langs[0]);
			__debug(__CLASS__."::__construct() - active_language from browser:".$this->active_language,__FILE__,__LINE__);
		}else{
			$this->active_language = __param("language","active");
			__debug(__CLASS__."::__construct() - active_language from parameter:".$this->active_language,__FILE__,__LINE__);
		}
		$this->encode = __param("system","encode");
		
		if(file_exists(dirname($_SERVER['SCRIPT_FILENAME'])."/webapp/i18n/".$this->active_language."/main.ini")){
			$this->messages = parse_ini_file(dirname($_SERVER['SCRIPT_FILENAME'])."/webapp/i18n/".$this->active_language."/main.ini",true);
			
		}else{
			$this->active_language = "en_en";	
			$this->messages = parse_ini_file(dirname($_SERVER['SCRIPT_FILENAME'])."/webapp/i18n/".$this->active_language."/main.ini",true);
		}
	}
	/**
	 * Retrieve a specific message wich identifier id $page:$key
	 * @param $page
	 * @param $key
	 * @return string of the message.
	 */
	public function getMessage($page,$key){
		if(!$this->messages[$page]) return "I18n: Translation for Page ($page) doesn't exist.";
		if(!$this->messages[$page][$key]) return "I18n: Translation for [$page|$key] doesn't exist.";
		return mb_convert_encoding($this->messages[$page][$key],$this->encode);
	}
	public function getParameterizedMessage($page,$key,$args){
		if(!$this->messages[$page]) return "I18n: Translation for Page ($page) doesn't exist.";
		if(!$this->messages[$page][$key]) return "I18n: Translation for [$page|$key] doesn't exist.";
		$message = mb_convert_encoding($this->messages[$page][$key],$this->encode);
		for($i=0;$i < count($args); $i++){
			$message = str_replace("{".$i."}",$args[$i],$message);
		}
		return $message;
	}
	public function getInstance(){
		if(empty(I18n::$_instance)){
			I18N::$_instance=new I18n();
		}
		return I18N::$_instance;
	}
	public function setLanguage($lang){
		$this->active_language=$lang;
	}
	public function getLanguage(){
		return $this->active_language;
	}
	public function addTemplateMessage($messages){
		$this->messages = array_merge($this->messages,$messages);
	}
}
?>